package com.local.admin.config;

import com.local.admin.interceptor.AdminInterceptor;
import com.local.common.constants.CommonConstants;
import io.lettuce.core.ReadFrom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.data.redis.LettuceClientConfigurationBuilderCustomizer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 后台配置类
 */

@Configuration
//@EnableWebMvc
public class AdminServiceConfig implements WebMvcConfigurer {

    @Value("${upload.image.save-path}")
    private String imageSavePath;

    @Value("${resource.dist}")
    private String distPath;

    @Value("${resource.plugins}")
    private String pluginsPath;
    /**
     * 后台拦截器类
     */
    @Autowired
    private AdminInterceptor adminInterceptor;

    /**
     * 添加拦截器
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(adminInterceptor)
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin/common/**")
                .excludePathPatterns("/admin/login")
                .excludePathPatterns("/admin/logout")
                .excludePathPatterns("/admin/dist/**")
                .excludePathPatterns("/admin/plugins/**");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/images/**").addResourceLocations("file:" + this.imageSavePath);
//        registry.addResourceHandler("/admin/dist/**").addResourceLocations("file:" + this.distPath);
//        registry.addResourceHandler("/admin/plugins/**").addResourceLocations("file:" + this.pluginsPath);
    }


//    @Bean //redis哨兵模式下的读写分离配置
    LettuceClientConfigurationBuilderCustomizer configurationBuilderCustomizer() {
        return new LettuceClientConfigurationBuilderCustomizer() {
            @Override
            public void customize(LettuceClientConfiguration.LettuceClientConfigurationBuilder clientConfigurationBuilder) {
                clientConfigurationBuilder.readFrom(ReadFrom.REPLICA_PREFERRED);
            }
        };
    }

}
